package com.timmy.google_play.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.timmy.google_play.base.BaseFragment;
import com.timmy.google_play.base.LoadingPager;
import com.timmy.google_play.protocol.RecommendProtocol;
import com.timmy.google_play.utils.ShakeListener;
import com.timmy.google_play.utils.UIUtils;
import com.timmy.google_play.views.flyinflyout.StellarMap;

import java.util.List;
import java.util.Random;

/**
 * Created by Timmy on 2016/12/7.
 */
public class RecommendFragment extends BaseFragment {

    public  List<String>  mDatas;
    private ShakeListener mShakeListener;

    @Override
    protected LoadingPager.LoadedResult initData() {

        RecommendProtocol protocol = new RecommendProtocol();

        try {
            mDatas = protocol.loadData(0);

            return checkResData(mDatas);
        } catch (Exception e) {
            e.printStackTrace();

            return LoadingPager.LoadedResult.ERROR;
        }
    }

    @Override
    protected View initSuccessView() {

        final StellarMap stellarMap = new StellarMap(UIUtils.getContext());

        final RecommendAdapter recommendAdapter = new RecommendAdapter();

        stellarMap.setAdapter(recommendAdapter);

        //1.解决首页空白问题
        stellarMap.setGroup(0, true);

        //2.每页显示15个
        stellarMap.setRegularity(15, 50);

        //摇一摇换到下一页

        mShakeListener = new ShakeListener(UIUtils.getContext());
        mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
            @Override
            public void onShake() {

                int currentGroup = stellarMap.getCurrentGroup();

                if (currentGroup == recommendAdapter.getGroupCount() - 1) { // 最后一页

                    currentGroup = 0;

                } else {
                    currentGroup++;
                }

                stellarMap.setGroup(currentGroup, true);

            }
        });

        return stellarMap;
    }

    class RecommendAdapter implements StellarMap.Adapter {
        public static final int PAGESIZE = 15;

        @Override
        public int getGroupCount() {

            int pageSize = mDatas.size() / PAGESIZE;

            if (mDatas.size() % PAGESIZE != 0) {
                pageSize++;
            }

            return pageSize;
        }

        @Override
        public int getCount(int group) {

            if (mDatas.size() % PAGESIZE == 0) {
                return PAGESIZE;
            } else {
                if (group == getGroupCount() - 1) {
                    return mDatas.size() % PAGESIZE;
                } else {
                    return PAGESIZE;
                }
            }
        }

        @Override
        public View getView(int group, int position, View convertView) {

            TextView tv = new TextView(UIUtils.getContext());

            int index = group * PAGESIZE + position;

            String data = mDatas.get(index);

            tv.setText(data);
//            tv.setText("郑瑞丽大傻逼");

            //随机字体大小,随机颜色
            Random random = new Random();

            int size = random.nextInt(10) + 5;
            tv.setTextSize(size);

            //颜色

            int alpha = 255;
            int red   = random.nextInt(150) + 50;
            int green = random.nextInt(150) + 50;
            int blue  = random.nextInt(150) + 50;
            int color = Color.argb(alpha, red, green, blue);

            tv.setTextColor(color);

            return tv;
        }

        @Override
        public int getNextGroupOnPan(int group, float degree) {
            return 0;
        }

        @Override
        public int getNextGroupOnZoom(int group, boolean isZoomIn) {
            return 0;
        }
    }

    @Override
    public void onResume() {
        if (mShakeListener != null) {
            mShakeListener.resume();
        }

        super.onResume();
    }

    @Override
    public void onPause() {
        if (mShakeListener != null) {
            mShakeListener.pause();
        }

        super.onPause();
    }
}
